package com.kehutong.crm.admin;

import java.util.List;
import org.coraframework.authz.HasPermission;
import org.coraframework.bean.valid.ValidResult;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.common.entity.Root;
import com.kehutong.crm.entity.WordsSharePlatform;
/**
 * 话术库控制器
 * @author: liukunlong
 * @date: 2021-02-19 15:12
 */
@MvcAction
@ReqMapping("/admin/crm/words/platform")
public class WordsPlatformController {
	
	@Inject
	private JdbcSession jdbcSession;

	@HasPermission("crm:words:view")
	@ReqMapping("/tree")
	public Object listParent(JSONObject jsonObject) {
		final List<WordsSharePlatform> tagList = jdbcSession.findArray(WordsSharePlatform.class)
															.eq("pid", Root.ROOT_ID)
															.eq("isFolder", true)
															.eq("deleted", false)
															.exe();
		WordsSharePlatform root = Pool.newInstance(WordsSharePlatform.class, Root.ROOT_ID);
		root.setName("话术库");
		tagList.forEach(tag->{
			root.addChild(tag);
		});

		return Result.success(root);
	}

	@HasPermission("crm:words:view")
	@ReqMapping("/getAllList")
	public Object getAllList(JSONObject jsonObject) {
		final List<WordsSharePlatform> wordsShareList = jdbcSession.findArray(WordsSharePlatform.class)
				.eq("pid", Root.ROOT_ID)
				.eq("isFolder", false)
				.eq("deleted", false)
				.exe();

		wordsShareList.forEach(wordsShare->{
			List<WordsSharePlatform> wordsShares = jdbcSession.findArray(WordsSharePlatform.class)
					.eq("pid", wordsShare.getId())
					.eq("deleted", false)
					.exe();
			wordsShare.setChildList(wordsShares);
		});
		return Result.success(wordsShareList);
	}

    @HasPermission("crm:words:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
    	String pid = jsonObject.getString("pid");
		if (Objects.isEmpty(pid) || Root.isRoot(pid)) {
			jsonObject.remove("pid");
		}
		
        return jdbcSession.findPage(WordsSharePlatform.class)
        		.eq(jsonObject, "id", "pid")
				.eq("isFolder", false)
            	.eq("deleted", false)
                .like(jsonObject, "name")
                .order("createTime", "desc")
                .page(jsonObject)
                .exe();
    }

    @HasPermission("crm:words:view")
    @ReqMapping("/get")
    public Object get(String id) {
		WordsSharePlatform item = Pool.get(WordsSharePlatform.class, id);
    	return Result.success(item);
    }

	@Bean(newInstance=true, copy=true)
    @HasPermission("crm:words:edit")
    @ReqMapping("/save")
    public Object save(WordsSharePlatform wordsShare, ValidResult result) {
    	if (result.hasError("userId")) {
    		return Result.error(0, result.getErrorMessage());
    	}
    	
    	jdbcSession.insert(wordsShare);
		if (Root.isRoot(wordsShare.getPid())) {
			return Result.success(wordsShare);
		}
    	return Result.success();
    }

	@Bean(copy=true)
    @HasPermission("crm:words:edit")
    @ReqMapping("/update")
    public Object update(WordsSharePlatform item, ValidResult result) {
    	if (result.hasError("userId")) {
    		return Result.error(0, result.getErrorMessage());
    	}

		WordsSharePlatform wordsShare = Pool.get(WordsSharePlatform.class, item.getId());
		if (Objects.isNull(wordsShare)) {
			return Result.error(-1, "该文件夹不存在！");
		}
		item.setFolder(wordsShare.isFolder());
    	
    	jdbcSession.updateById(item);
    	return Result.success();
    }

	@HasPermission("crm:words:edit")
	@ReqMapping("/delete")
	public Object delete(String id) throws Exception {
		WordsSharePlatform wordsShare = Pool.get(WordsSharePlatform.class, id);
		if (Root.isRoot(wordsShare.getPid())) {

			jdbcSession.deleteById(wordsShare);
			List<WordsSharePlatform> list = jdbcSession.findArray(WordsSharePlatform.class)
					.eq("pid", wordsShare.getId())
					.eq("deleted", false)
					.exe();
			jdbcSession.deleteById(list);
		} else {
			jdbcSession.deleteById(wordsShare);
		}
		return Result.success();
	}

}
